﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using web.data.entities;
using System.Configuration;
using Webdiyer.WebControls.Mvc;
using System.Web.UI;
using System.Linq.Expressions;
using System.Reflection;
using System.Xml.Schema;

namespace web.bussines
{
    public class checkout_service
    {
        public int createOrder(order ord, List<order_detail> list_order_detail)
        {
            using (var data = new baseEntities())
            {
                foreach (var item in list_order_detail)
                {
                    ord.tbl_order_detail.Add(item);
                }
                data.AddTotbl_order(ord);
                data.SaveChanges();

                return ord.order_id;
            }
        }

        public customer GetCustomerByUserID(int user_id)
        {
            using (var data = new baseEntities())
            {
                var cus = data.tbl_customer.Where(p => p.user_id == user_id);
                
                return cus.FirstOrDefault();
               
            }
        }


        public order GetOrderByID(int order_id)
        {
            using (var data = new baseEntities())
            {
                var ord = data.tbl_order.Where(p => p.order_id == order_id);

                return ord.FirstOrDefault();

            }
        }

        public order GetLastOrderPendingByUserID(int user_id)
        {
            using (var data = new baseEntities())
            {
                var ord = data.tbl_order.Where(p => p.user_id == user_id && p.payment_status == 0).OrderByDescending(p=>p.order_id).FirstOrDefault();

                return ord;

            }
        }

        public void UpdatePaymentMenthodForOrder(order ord)
        {
            using (var data = new baseEntities())
            {
                var edit = data.tbl_order.Where(p => p.order_id == ord.order_id).FirstOrDefault();

                if (edit != null) {
                    edit.payment_type = ord.payment_type;
                }
            }
        }

        public List<proc_order_result> GetOrderDetailByOrderHistory(int order_id,int language_id)
        {
            using (var data = new baseEntities())
            {
                var list_order = data.proc_order(order_id, language_id).ToList();

                return list_order;
            }
        }

        public List<order> GetListOrderByMember(int user_id)
        {
            using (var data = new baseEntities())
            {
                var list_order = (from p in data.tbl_order
                                  where p.user_id == user_id
                                  select p);
                list_order = list_order.OrderByDescending(p => p.order_id);

                return list_order.ToList();
            }
        }
    }
}
